package inputWindow;

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

public class CreateWindow extends JFrame implements ActionListener{

	private JPanel contentPane;
	private String filename;
	private String code;
	private JTextField programName;
	private JTextArea codeArea;
	private JButton closeButton;
	private JButton saveButton;
	private JButton openButton;
	private File openProgram;
	private File newProgram;
	private JFileChooser fc = new JFileChooser();
	private FileManager manager = new FileManager();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) throws IOException{
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					CreateWindow frame = new CreateWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public CreateWindow() {
		setTitle("Create/Edit Program");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		GridBagLayout gbl_contentPane = new GridBagLayout();
		gbl_contentPane.columnWidths = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0};
		gbl_contentPane.columnWeights = new double[]{0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gbl_contentPane.rowWeights = new double[]{0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPane.setLayout(gbl_contentPane);
		
		JLabel lblEnterCode = new JLabel("Enter Code:");
		GridBagConstraints gbc_lblEnterCode = new GridBagConstraints();
		gbc_lblEnterCode.gridwidth = 12;
		gbc_lblEnterCode.anchor = GridBagConstraints.NORTHWEST;
		gbc_lblEnterCode.insets = new Insets(0, 0, 5, 5);
		gbc_lblEnterCode.gridx = 2;
		gbc_lblEnterCode.gridy = 0;
		contentPane.add(lblEnterCode, gbc_lblEnterCode);
		
		codeArea = new JTextArea();
		codeArea.setLineWrap(true);
		
		JScrollPane panel = new JScrollPane(codeArea);
		//panel.setBounds(0,0,410,200);
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.gridheight = 4;
		gbc_panel.gridwidth = 12;
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 2;
		gbc_panel.gridy = 1;
		contentPane.add(panel, gbc_panel);
		
		
		
		JLabel lblFilename = new JLabel("Program Name");
		GridBagConstraints gbc_lblFilename = new GridBagConstraints();
		gbc_lblFilename.anchor = GridBagConstraints.NORTHWEST;
		gbc_lblFilename.insets = new Insets(0, 0, 5, 5);
		gbc_lblFilename.gridx = 2;
		gbc_lblFilename.gridy = 5;
		contentPane.add(lblFilename, gbc_lblFilename);
		
		programName = new JTextField();
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.gridwidth = 12;
		gbc_textField.insets = new Insets(0, 0, 5, 5);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 2;
		gbc_textField.gridy = 6;
		contentPane.add(programName, gbc_textField);
		programName.setColumns(10);
		
		closeButton = new JButton("Close");
		closeButton.addActionListener(this);
		GridBagConstraints gbc_closeButton = new GridBagConstraints();
		gbc_closeButton.anchor = GridBagConstraints.WEST;
		gbc_closeButton.insets = new Insets(0, 0, 0, 5);
		gbc_closeButton.gridx = 2;
		gbc_closeButton.gridy = 7;
		contentPane.add(closeButton, gbc_closeButton);
		
		saveButton = new JButton("Save");
		saveButton.addActionListener(this);
		
		openButton = new JButton("Open");
		GridBagConstraints gbc_openButton = new GridBagConstraints();
		gbc_openButton.insets = new Insets(0, 0, 0, 5);
		gbc_openButton.gridx = 12;
		gbc_openButton.gridy = 7;
		openButton.addActionListener(this);
		contentPane.add(openButton, gbc_openButton);
		GridBagConstraints gbc_saveButton = new GridBagConstraints();
		gbc_saveButton.insets = new Insets(0, 0, 0, 5);
		gbc_saveButton.anchor = GridBagConstraints.EAST;
		gbc_saveButton.gridx = 13;
		gbc_saveButton.gridy = 7;
		contentPane.add(saveButton, gbc_saveButton);
		
		
		
		
	}

	@Override
	public void actionPerformed(ActionEvent e){
		// TODO Auto-generated method stub
		
		if (e.getSource() == openButton) 
		{
            int returnVal = fc.showOpenDialog(CreateWindow.this);

            if (returnVal == JFileChooser.APPROVE_OPTION) 
            {
                openProgram = fc.getSelectedFile();
                programName.setText(openProgram.getName());
                try {
					codeArea.setText(manager.copyFromFile(openProgram));
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
            }
		
		}
		
		
		if(e.getSource() == saveButton)
		{
				filename = programName.getText();
				code = codeArea.getText();
				
				try {
					newProgram = manager.createFile(filename);
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
					try {
						manager.copyToFile(newProgram, code);
					} catch (IOException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
		}
		
		if(e.getSource() == closeButton)
		{
			
			this.dispose();
		}
		
		
	}
	
	}


